#include "dircmd2_sql.h"
#include "dirStack.h"
#include "msgHandler.h"

//返回成功信息
int return_succss_msg(int netfd,train_t *pTrain)
{

    pTrain->length = strlen(RET_SUCCESS);
    memcpy(pTrain->data, RET_SUCCESS, pTrain->length);
    send_msg(netfd, pTrain);
    return 0;
}

//返回错误信息
int return_fail_msg(int netfd,train_t *pTrain)
{
    pTrain->length = strlen(RET_FAIL);
    memcpy(pTrain->data, RET_FAIL, pTrain->length);
    send_msg(netfd, pTrain);
    return 0;
}

int myMkdir(int netfd, dirStack_t *pStack, char *args,char *user,MYSQL *mysql)
{
    printf("args =%s\n", args);
    char type[]="1";

    char id[512] = {0};
    char sql_buf[4096] = {0};
    memcpy(id, pStack->pFront->data, strlen(pStack->pFront->data));
    char query_path[] = "select filepath from file_info where";
    snprintf(sql_buf, sizeof(sql_buf), "%s id = '%s';", query_path, id);
    mysql_query(mysql,sql_buf);
    MYSQL_RES *result = mysql_store_result(mysql);
    MYSQL_ROW row=mysql_fetch_row(result);
    char path[1024] = {0};
    memcpy(path, row[0], strlen(row[0]));
    if(strcmp(row[0], "/") == 0) {
        sprintf(path,"%s%s", row[0], args);
    } else {
        sprintf(path,"%s/%s", row[0], args);
    }
    printf("path = %s\n", path);
    //判断在数据库里面是否同名目录,如果有则返回错误信息，如果没有则进行信息插入
    //发送查询的信息，判断是否有同名目录
    char inqu_per_sql[]="select count(*) from file_info where";
    bzero(sql_buf, sizeof(sql_buf));
    snprintf(sql_buf, sizeof(sql_buf), "%s filepath='%s' and user='%s' and pre_id='%s' and type='%s' and tomb = 0;",inqu_per_sql,path,user,id,type);
    mysql_query(mysql,sql_buf);
    //判断counts是否是0
    result = mysql_store_result(mysql);
    row=mysql_fetch_row(result);
    if(row!=NULL&&strcmp(row[0],"0")==0)
    {
        //拼接要插入数据库的信息
        char per_sql[]="insert into file_info (filename,filepath,user,pre_id,type,tomb) values ";
        char sql[4096];
        bzero(sql,sizeof(sql));
        snprintf(sql, sizeof(sql), "%s('%s', '%s', '%s', '%s', '1', '%d');",per_sql,args,path,user,id,0);

        //发送信息
        int qret=mysql_query(mysql,sql);
        //返回消息
        train_t train_sendinfo;
        bzero(&train_sendinfo,sizeof(train_t));
        if(qret!=0)
        {
            fprintf(stderr,"mysql_query:%s\n",mysql_error(mysql));
            return_fail_msg(netfd,&train_sendinfo);
        }
        else
        {
            return_succss_msg(netfd,&train_sendinfo);
        }
    }
    else
    {
        train_t train_msg;
        return_fail_msg(netfd,&train_msg);
    }

    return 0;
}

int myRmdir(int netfd, dirStack_t *pStack, char *args,char *user,MYSQL *mysql)
{
    char type[]="1";

    char id[512] = {0};
    char sql_buf[4096] = {0};
    memcpy(id, pStack->pFront->data, strlen(pStack->pFront->data));
    char query_path[] = "select filepath from file_info where";
    snprintf(sql_buf, sizeof(sql_buf), "%s id = '%s';", query_path, id);
    mysql_query(mysql,sql_buf);
    MYSQL_RES *result = mysql_store_result(mysql);
    MYSQL_ROW row=mysql_fetch_row(result);
    char path[1024] = {0};
    memcpy(path, row[0], strlen(row[0]));
    printf("row[0]=%s\n",row[0]);
    if(strcmp(row[0], "/") == 0) {
        sprintf(path,"%s%s", row[0], args);
    } else {
        sprintf(path,"%s/%s", row[0], args);
    }
    printf("path = %s\n", path);

    //查询要删除目录的id
    char buf1[] = "select id from file_info where";
    bzero(sql_buf, sizeof(sql_buf));
    snprintf(sql_buf, sizeof(sql_buf), "%s filepath = '%s'and user='%s'and type='1'and tomb='0';", buf1, path,user);
    mysql_query(mysql,sql_buf);
    result = mysql_store_result(mysql);
    row=mysql_fetch_row(result);
    if(!row){
        train_t train_sendinfo;
        bzero(&train_sendinfo,sizeof(train_t));
        return_fail_msg(netfd,&train_sendinfo);
    } else {
        char dir_id[1024] = {0};
        memcpy(dir_id, row[0], strlen(row[0]));
        //判断是否是空目录
        char sel_pre_sql[]="select count(*) from file_info where";
        char sel_sql[4096];
        bzero(sel_sql,sizeof(sel_sql));
        snprintf(sel_sql,sizeof(sel_sql), "%s user='%s' and pre_id='%s'and tomb=0;",sel_pre_sql,user,row[0]);
        printf("sel_sql=%s\n",sel_sql);
        mysql_query(mysql,sel_sql);
        MYSQL_RES *result_new=mysql_store_result(mysql);
        MYSQL_ROW row_new=mysql_fetch_row(result_new);

        /* printf("row_new[0]=%s\n",row_new[0]); */
        printf("row_new[0]=%s\n",row_new[0]);
        if(row_new!=NULL&&strcmp(row_new[0],"0")==0)
        {//判断成功是空目录，更新文件信息
            char upda_pre_sql[]="update file_info set tomb = '1' where";
            char upda_sql[4096];
            bzero(upda_sql,sizeof(upda_sql));

            snprintf(upda_sql, sizeof(upda_sql), "%s id='%s';", upda_pre_sql, dir_id); 
            printf("upda_sql=%s\n",upda_sql);
            /* printf("upda_sql=%s\n",upda_sql); */
            //发送更新信息
            int qret=mysql_query(mysql,upda_sql);

            train_t train_sendinfo;
            bzero(&train_sendinfo,sizeof(train_t));
            if(qret!=0)
            {
                fprintf(stderr,"mysql_query:%s\n",mysql_error(mysql));
                return_fail_msg(netfd,&train_sendinfo);
            }
            else
            {
                return_succss_msg(netfd,&train_sendinfo);
            }
        }//不是空目录或者row==NULL,返回错误信息
        else
        {
            train_t train_sendinfo;
            bzero(&train_sendinfo,sizeof(train_t));
            return_fail_msg(netfd,&train_sendinfo);
        }
    }
    return 0;
}

int myRm(int netfd, dirStack_t *pStack, char *args,char *user,MYSQL *mysql)
{
    char type[]="0";
    char id[512] = {0};
    char sql_buf[4096] = {0};
    memcpy(id, pStack->pFront->data, strlen(pStack->pFront->data));
    char query_path[] = "select filepath from file_info where";
    snprintf(sql_buf, sizeof(sql_buf), "%s id = '%s';", query_path, id);
    mysql_query(mysql,sql_buf);
    MYSQL_RES *result = mysql_store_result(mysql);
    MYSQL_ROW row=mysql_fetch_row(result);
    char path[1024] = {0};
    memcpy(path, row[0], strlen(row[0]));
    printf("row[0]=%s\n",row[0]);
    if(strcmp(row[0], "/") == 0) {
        sprintf(path,"%s%s", row[0], args);
    } else {
        sprintf(path,"%s/%s", row[0], args);
    }
    printf("path = %s\n", path);

    //查询要删除目录的id
    char buf1[] = "select id from file_info where";
    bzero(sql_buf, sizeof(sql_buf));
    snprintf(sql_buf, sizeof(sql_buf), "%s filepath = '%s'and user='%s'and type='0'and tomb='0';", buf1, path,user);
    mysql_query(mysql,sql_buf);
    result = mysql_store_result(mysql);
    row=mysql_fetch_row(result);
    if(row!=NULL){

        char dir_id[1024] = {0};
        memcpy(dir_id, row[0], strlen(row[0]));

        char buf2[] = "update file_info set tomb = 1 where";
        bzero(sql_buf, sizeof(sql_buf));
        snprintf(sql_buf, sizeof(sql_buf), "%s id = '%s' and user = '%s' ;", buf2,row[0],user);
        printf("sql_buf=%s\n",sql_buf);
        int qret = mysql_query(mysql,sql_buf);

        train_t train_sendinfo;
        bzero(&train_sendinfo,sizeof(train_t));
        int affected_rows = mysql_affected_rows(mysql);
        if(qret!=0 || affected_rows == 0)
        {
            fprintf(stderr,"mysql_query:%s\n",mysql_error(mysql));
            return_fail_msg(netfd,&train_sendinfo);
        }
        else
        {
            return_succss_msg(netfd,&train_sendinfo);
        }

    }
    else
    {
        train_t train_sendinfo;
        bzero(&train_sendinfo,sizeof(train_t));
        return_fail_msg(netfd,&train_sendinfo);
    }
    return 0;
}
